package us.mooresr.aircraftscheduling.client.ui;

import us.mooresr.aircraftscheduling.client.events.UserLoggedInEvent;
import us.mooresr.aircraftscheduling.client.events.UserLoggedOutEvent;
import us.mooresr.aircraftscheduling.client.place.NameTokens;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PushButton;
import com.google.gwt.user.client.ui.Widget;
import com.google.inject.Inject;
import com.gwtplatform.mvp.client.proxy.PlaceManager;
import com.gwtplatform.mvp.client.proxy.PlaceRequest;

public class MainMenu extends Composite {
	MainPagePresenter presenter;

	private static MainMenuUiBinder uiBinder = GWT
			.create(MainMenuUiBinder.class);

	interface MainMenuUiBinder extends UiBinder<Widget, MainMenu> {
	}
	
	private PlaceManager placeManager;
	
	@UiField
	HorizontalPanel hPanel;
	
	@UiField
	PushButton userlistButton;
	
	@UiField
	PushButton reservationlistButton;
	
	@UiField
	Label usernameLabel;
	
	@UiField
	PushButton logoutButton;

	public MainMenu(PlaceManager placeManager) {
		this.placeManager = placeManager;
		
		initWidget(uiBinder.createAndBindUi(this));
		
		hPanel.setWidth("100%");
		
		reservationlistButton.addStyleName("selectedMenuButton");
		userlistButton.addStyleName("unselectedMenuButton");
	}

	@UiHandler("userlistButton")
	void onUserClick(ClickEvent event) {
		userlistButton.addStyleName("selectedMenuButton");
		userlistButton.removeStyleName("unselectedMenuButton");
		reservationlistButton.removeStyleName("selectedMenuButton");
		reservationlistButton.addStyleName("unselectedMenuButton");
		PlaceRequest placeRequest = new PlaceRequest(NameTokens.userlist);
		placeManager.revealPlace(placeRequest);
	}
	
	@UiHandler("reservationlistButton")
	void onReservationClick(ClickEvent event) {
		userlistButton.addStyleName("unselectedMenuButton");
		userlistButton.removeStyleName("selectedMenuButton");
		reservationlistButton.addStyleName("selectedMenuButton");
		reservationlistButton.removeStyleName("unselectedMenuButton");
		PlaceRequest placeRequest = new PlaceRequest(NameTokens.reservationlist);
		placeManager.revealPlace(placeRequest);
	}
	
	@UiHandler("logoutButton")
	void onLogoutClick(ClickEvent event) {
		
		presenter.doLogout();
		
	}

	public void showAdminFunctions() {
		// userlistButton.setVisible(true);
		
	}

	public void hideAdminFunctions() {
		// userlistButton.setVisible(false);
		
	}

	public void setUsername(String currentUserName) {
		usernameLabel.setText("Logged in as " + currentUserName);
		
	}

	public void setPresenter(MainPagePresenter presenter) {
		this.presenter = presenter;
	}
}

